package com.ph.shopping.facade.member.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.member.dto.IcCradindInfoDto;
import com.ph.shopping.facade.member.dto.MemberIcCardInfoPageDto;
import com.ph.shopping.facade.member.dto.UnbundMemberCardDto;
import com.ph.shopping.facade.member.entity.IcCardInfo;
import com.ph.shopping.facade.member.service.IMemberCardService;
import com.ph.shopping.facade.member.service.dbservice.facade.IMemberCardDbService;

@Component
@Service(version = "1.0.0")
public class MemberCardService implements IMemberCardService {

	private static final Logger logger = LoggerFactory.getLogger(MemberCardService.class);

	@Autowired
	private IMemberCardDbService memberCardDbService;

	@Override
	public Result addMemberCard(IcCardInfo icInfo) {
		logger.info("添加会员卡参数,IcCardInfo = {} ", JSON.toJSONString(icInfo));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "innerCode", "outerCode" };
			if (ParamVerifyUtil.entityIsNotNullByField(icInfo, fields)) {
				result = memberCardDbService.addMemberCard(icInfo);
			}
		} catch (Exception e) {
			logger.error("添加会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result bindMemberCard(IcCradindInfoDto dto) {
		logger.info("会员绑定会员卡参数,IcCradindInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "phone", "merchantId" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.bindMemberCard(dto);
			}
		} catch (Exception e) {
			logger.error("绑定会员卡信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result cancelMemberCard(UnbundMemberCardDto dto) {
		logger.info("会员挂失解绑会员卡参数,UnbundMemberCardDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "phone", "operatorUserId" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.cancelMemberCard(dto);
			}
		} catch (Exception e) {
			logger.error("挂失解绑会员卡错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberBindCardByPage(MemberIcCardInfoPageDto dto) {
		logger.info("根据分页信息获取会员绑定的会员卡列表,MemberIcCardInfoPageDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "merchantId", "pageNum", "pageSize" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.queryMemberBindCardByPage(dto);
			}
		} catch (Exception e) {
			logger.error("获取会员绑定的会员卡列表失败", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberIcCardInfoByPhone(String Phone) {
		logger.info("根据手机号码获取会员卡信息,Phone >> ", Phone);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(Phone)) {
				result = memberCardDbService.queryMemberIcCardInfoByPhone(Phone);
			}
		} catch (Exception e) {
			logger.error("根据手机号获取会员绑定的会卡数据错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberBindCardByPageAndAll(MemberIcCardInfoPageDto dto) {
		logger.info("根据分页信息获取会员绑定的会员卡列表,MemberIcCardInfoPageDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "pageNum", "pageSize" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.getMemberBindCardByPageAndAll(dto);
			}
		} catch (Exception e) {
			logger.error("获取会员绑定的会员卡列表失败", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result frozenMemberCard(UnbundMemberCardDto dto) {
		logger.info("冻结会员卡参数,UnbundMemberCardDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "icCardId" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.frozenMemberCard(dto);
			}
		} catch (Exception e) {
			logger.error("冻结会员卡错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberCardByPage(MemberIcCardInfoPageDto dto) {
		logger.info("加载会员卡列表参数,MemberIcCardInfoPageDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "pageNum", "pageSize" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.queryMemberCardByPage(dto);
			}
		} catch (Exception e) {
			logger.error("加载会员卡列表错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result relieveMemberCard(UnbundMemberCardDto dto) {
		logger.info("解冻会员卡参数,UnbundMemberCardDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "icCardId" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.relieveMemberCard(dto);
			}
		} catch (Exception e) {
			logger.error("解冻会员卡错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result batchAddMemberCard(List<IcCardInfo> list) {
		logger.info("批量添加会员卡参数,IcCardInfo = {} ", JSON.toJSONString(list));
		Result result = DubboResult.getDefResult();
		try {
			result = memberCardDbService.batchAddMemberCard(list);
		} catch (Exception e) {
			logger.error("批量添加会员卡错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}
	
	@Override
	public Result getMemberCardInfoByCode(IcCradindInfoDto dto) {
		logger.info("根据卡号查询会员信息参数,IcCradindInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "innerCode" , "outerCode" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberCardDbService.queryMemberCardInfoByCode(dto);
			}
		} catch (Exception e) {
			logger.error("根据卡号查询会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberCardInfoByInnerCode(String innerCode) {
		logger.info("根据内码查询会员信息参数,innerCode " + innerCode);
		Result result = DubboResult.getDefResult();
		try {
			if(StringUtils.isNotBlank(innerCode)){
				result = memberCardDbService.queryMemberCardInfoByInnerCode(innerCode);
			}
		} catch (Exception e) {
			logger.error("根据内码查询会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberCardInfoByOuterCode(String outerCode) {
		logger.info("根据外码查询会员信息参数,outerCode " + outerCode);
		Result result = DubboResult.getDefResult();
		try {
			if(StringUtils.isNotBlank(outerCode)){
				result = memberCardDbService.queryMemberCardInfoByOuterCode(outerCode);
			}
		} catch (Exception e) {
			logger.error("根据外码查询会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

}
